package com.application.meizhi.ui.activity.douban;

import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.animation.DecelerateInterpolator;

import com.application.meizhi.R;
import com.application.meizhi.adapter.DouBanMeiZhiDetailAdapter;

import com.application.meizhi.model.douban.DouBanMeiZhi;
import com.application.meizhi.persenter.douban.DouBanDetailActivityPresenter;
import com.application.meizhi.ui.activity.BaseActivity;
import com.application.meizhi.ui.iView.douban.IDouBanDetailView;


import java.util.ArrayList;

import butterknife.BindView;

public class DouBanDetailActivity extends BaseActivity<DouBanDetailActivityPresenter> implements IDouBanDetailView {

    public static final String ARG_PARAM1 = "param1";
    public static final String ARG_PARAM2 = "param2";
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appBar)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.viewpager)
    ViewPager mViewPager;

    private DouBanMeiZhiDetailAdapter mAdapter;

    private ArrayList<DouBanMeiZhi> list;

    private ActionBar actionBar;

    private boolean isHide = false;

    private int position;


    @Override
    protected void initPresenter() {

        presenter = new DouBanDetailActivityPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_douban_detail;
    }

    @Override
    public void initView() {
        list = (ArrayList<DouBanMeiZhi>) getIntent().getSerializableExtra(ARG_PARAM1);
        position = getIntent().getIntExtra(ARG_PARAM2, 0);

        DouBanMeiZhi douBanMeiZhi=list.get(position);

        toolbar.setTitle(douBanMeiZhi.getTitle());
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayHomeAsUpEnabled(true);


        mAdapter = new DouBanMeiZhiDetailAdapter(list, this);
        mViewPager.setAdapter(mAdapter);

        mViewPager.setCurrentItem(position);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                toolbar.setTitle(list.get(position).getTitle());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void hideOrShowToolbar() {

        if (isHide) {
            //显示
            mAppBarLayout.animate()
                    .translationY(0)
                    .setInterpolator(new DecelerateInterpolator(2))
                    .start();
            isHide = false;
        } else {
            //隐藏
            mAppBarLayout.animate()
                    .translationY(-mAppBarLayout.getHeight())
                    .setInterpolator(new DecelerateInterpolator(2))
                    .start();
            isHide = true;
        }
    }
}
